package com.ruoyi.com.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.com.mapper.ComUserMapper;
import com.ruoyi.com.service.IComUserService;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.OperateBasicForm;
import com.ruoyi.common.core.domain.entity.ComUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.domain.vo.ComUserVo;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公共用户Service业务层处理
 *
 * @author sunye
 * @date 2024-09-26
 */
@Service
public class ComUserServiceImpl extends ServiceImpl<ComUserMapper, ComUser> implements IComUserService
{
    @Autowired
    private ComUserMapper comUserMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Override
    public ComUser selectUserByUsername(String username) {
        return comUserMapper.selectAppUserByUserName(username);
    }

    @Override
    public List<ComUser> selectAllocatedList(ComUser user) {
        return comUserMapper.selectAllocatedList(user);
    }

    @Override
    public List<ComUser> selectUnallocatedList(ComUser user) {
        return comUserMapper.selectUnallocatedList(user);
    }

    @Override
    public void updateUserProfile(ComUser user) {
         comUserMapper.updateUserProfile(user);
    }

    @Override
    public int updateByUserId(ComUser currentUser) {
       return comUserMapper.updateByUserId(currentUser);
    }

    @Override
    public int resetUserPwd(String userName, String password) {
        return comUserMapper.resetUserPwd(userName, password);

    }

    @Override
    public List<ComUser> selectComUserList(ComUserVo comUser) {
        return comUserMapper.selectComUserList(comUser);
    }

    @Override
    public ComUser selectComUserByUserIdAndDeptId(Long userId,Long deptId)
    {
        ComUser comUser = comUserMapper.selectComUserByUserIdAndDeptId(userId,deptId);
        List<SysUserRole> list = userRoleMapper.selectUnifyUserRoleByUserId(userId);
        List<Long> roleIds = list.stream().map(item -> item.getRoleId()).collect(Collectors.toList());
        Long[] roleArray = roleIds.toArray(new Long[0]);
        comUser.setRoleIds(roleArray);
        return comUser;
    }

    @Override
    public int updateUser(ComUser comUser) {
        Long userId = comUser.getUserId();
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByUserId(userId);
        //新增用户和角色的关系
        insertUserRole(comUser);
        //修改用户
        comUser.setUpdateTime(DateUtils.getNowDate());
        return comUserMapper.updateByUserId(comUser);
    }


    /**
     * 新增用户角色信息
     *
     * @param comUser 用户对象
     */
    public void insertUserRole(ComUser comUser)
    {
        this.insertUserRole(comUser.getUserId(), comUser.getRoleIds());
    }


    /**
     * 新增用户角色信息
     *
     * @param userId 用户ID
     * @param roleIds 角色组
     */
    public void insertUserRole(Long userId, Long[] roleIds)
    {
        if (StringUtils.isNotEmpty(roleIds))
        {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>(roleIds.length);
            for (Long roleId : roleIds)
            {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(userId);
                ur.setRoleId(roleId);
                list.add(ur);
            }
            userRoleMapper.batchUserRole(list);
        }
    }
}
